package com.bluegalaxy.sitescore.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bluegalaxy.sitescore.entity.GroundWater;
import com.bluegalaxy.sitescore.entity.SelectDangerous;
import com.bluegalaxy.sitescore.entity.TbDangerous;
import com.bluegalaxy.sitescore.entity.TbGroundWater;
import com.bluegalaxy.sitescore.service.IGroundWaterService;
import com.bluegalaxy.sitescore.service.ITBSelectDangerousService;
import com.bluegalaxy.sitescore.service.ITbDangerousService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;


import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/groundWater")
public class GroundWaterController extends BaseController{


    @Autowired
    private IGroundWaterService groundWaterService;

    @Autowired
    private ITBSelectDangerousService itbSelectDangerousService;

    @Autowired
    private ITbDangerousService tbDangerousService;

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/index/{siteId}/{gid}")
    public String index(Model model,@PathVariable int siteId,@PathVariable int gid){
        int sourceId = siteId;
      //  List<SelectDangerous> selectDangerousList = itbSelectDangerousService
        List<SelectDangerous> selectDangerousList = itbSelectDangerousService.getDangerousBysid(sourceId,2);
        if (!selectDangerousList.isEmpty()){
            model.addAttribute("sdList",toJson(selectDangerousList));
        }else{
            List<TbDangerous> dangerousList = tbDangerousService.getDangerousSelected2(sourceId);
            model.addAttribute("sdList",toJson(dangerousList));
        }
        if(gid!=0){
            model.addAttribute("groundWater",groundWaterService.getGroundWaterById(gid,sourceId));
        }
        model.addAttribute("gid",gid);
        model.addAttribute("siteId",siteId);
        model.addAttribute("gList",toJson(groundWaterService.getGroundWaterList(sourceId)));
        return "/groundWater/add";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/caculateScore/{siteId}")
    public @ResponseBody String  caculateScore(GroundWater groundWater, Model model,@PathVariable int siteId){
        int source_id =siteId;

        JSONArray jsonArray= JSONArray.parseArray(groundWater.getSdList());
        List<SelectDangerous> selectDangerousList = new ArrayList<>();
        List<Integer> selectDangerousIdList = new ArrayList<>();
        //获得jsonArray的第一个元素
        for (int i =0;i<jsonArray.size();i++){
            Object o=jsonArray.get(i);
            SelectDangerous selectDangerous=(SelectDangerous) JSONObject.parseObject(JSONObject.toJSONString(o), SelectDangerous.class);
            if(selectDangerous.getDangerous_id()==0){
                selectDangerous.setDangerous_id(selectDangerous.getId());
            }else{
                selectDangerous.setDangerous_id(selectDangerous.getDangerous_id());
            }
            selectDangerous.setType(2);
            selectDangerous.setSource_id(source_id);
            selectDangerousList.add(selectDangerous);
            selectDangerousIdList.add(selectDangerous.getId());
       //     System.out.println(selectDangerous.getDangerous_id());
        }
        if( itbSelectDangerousService.getDangerousBysid(source_id,2).isEmpty()){
            if (!itbSelectDangerousService.insertDangerousList2(selectDangerousList)){
                return "false";
            }
        }else{
            if (itbSelectDangerousService.delDangerousListById(selectDangerousIdList)){
                if (!itbSelectDangerousService.insertDangerousList(selectDangerousList)){
                    return "false";
                }
            }else {
                return "false";
            }
        }
        /*if (itbSelectDangerousService.delDangerousListById(selectDangerousIdList)){
            if (!itbSelectDangerousService.insertDangerousList(selectDangerousList)){
                return "false";
            }
        }else {
            return "false";
        }*/

        if(groundWaterService.judegeGroundWater(groundWater)){
            if (groundWater.getId()!=0){
                if(groundWaterService.updateGroundWater(groundWater,source_id)){
                    return "success";
                }else {
                    return "false";
                }
            }else {
                if(groundWaterService.insertGroundWater(groundWater,source_id)){
                    return "success";
                }else {
                    return "false";
                }
            }
        }
        return "false";
    }
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping(value = "/delGroudScore",method = RequestMethod.POST)
    public @ResponseBody
    String GroundDel(int id, Model model,int siteId){
        int sourceId = siteId;
        //System.out.println(id+"---------------");
        if (groundWaterService.delGroundWater(id,sourceId)){
            return "{\"success\":\"删除成功\"}";
        }else {
            return "{\"false\":\"删除异常\"}";
        }

    }



    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/surface{id}")
    public String surface(Model model,@PathVariable String id) {
        return "/groundWater/surface"+id;
    }
}
